/**
 * @author Ahammer
 * @date 2009 04
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package opgui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.media.jai.ParameterBlockJAI;
import javax.media.jai.PlanarImage;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollBar;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import main.IqmTools;
import main.Tank;

public class IqmRGBRelativeGUI  extends IqmOperatorJFrame
							    implements IqmOperatorGUIBuilder, ActionListener, AdjustmentListener {

	private  ParameterBlockJAI    pb         = null;  //  @jve:decl-index=0:
		
	private  int          ratio;  //Slider Values

	private  JPanel 	jScrollBarPanelRatio    = null;
	private  JScrollBar	jScrollBarRatio         = null;
	private  JLabel     jLabelRatio;

	private  JRadioButtonMenuItem buttChR		= null;
	private  JRadioButtonMenuItem buttChG		= null;
	private  JRadioButtonMenuItem buttChB		= null;
	private  JRadioButtonMenuItem buttChHidden	= null;
	private  JLabel 			  jLabelText	= null;
	private  JToolBar 			  jToolBarChSelect  = null;
	private  ButtonGroup 		  buttGroupChSelect = null;
	private  JCheckBox 			  jCheckBoxBinarize	= null;
	
	/**
	 * constructor
	 */
	public IqmRGBRelativeGUI() {

	}


	/**
	 * This method sets the current parameter block to the argument
	 * @param ParameterBlockJAI pb
	 *
	 */
	@Override
	public void setParameterBlock(ParameterBlockJAI arg){
		pb = arg;
		IqmTools.setCurrParameterBlockJAI(pb);	
	}
	/**
	 * This method sets the current parameter block
	 * The individual values of the GUI current ParameterBlock
	 *
	 */
	@Override
	public void setParameterBlock(){
		if (buttChR.isSelected())     	pb.setParameter("Band", 0);
		if (buttChG.isSelected())     	pb.setParameter("Band", 1);
		if (buttChB.isSelected())    	pb.setParameter("Band", 2);
		pb.setParameter("Ratio", ratio);
		if (jCheckBoxBinarize.isSelected())    pb.setParameter("Binarize", 1);
		if (!jCheckBoxBinarize.isSelected())   pb.setParameter("Binarize", 0);
		IqmTools.setCurrParameterBlockJAI(pb);	
	}
	
	/**
	 * This method sets the current parameter values
	 */
	@Override
	public void setParameterValues(){
		int idx = IqmTools.getCurrManagerImgIndex();
	    PlanarImage pi = Tank.getCurrentTankImageAt(idx); 
	       if (pi == null){
	    	   System.out.println("IqmResizeGUI: Current image not defined");
	    	   return;
	       }
//	    int pixelSize = pi.getColorModel().getPixelSize();
//      int numBands  = pi.getNumBands(); 
//      String type = IqmTools.getImgTyp(pixelSize, numBands);
        String type = IqmTools.getImgTyp(pi);

		buttChHidden.setSelected(true);
		if (pb.getIntParameter(0) == 0 )  buttChR.setSelected(true);		
		if (pb.getIntParameter(0) == 1 )  buttChG.setSelected(true);		
		if (pb.getIntParameter(0) == 2 )  buttChB.setSelected(true);
		
		if (buttChR.isSelected()) jLabelText.setText("Ratio = R/(R+G+B)");
		if (buttChG.isSelected()) jLabelText.setText("Ratio = G/(R+G+B)");
		if (buttChB.isSelected()) jLabelText.setText("Ratio = B/(R+G+B)");
		
		ratio   = pb.getIntParameter("Ratio");
		jScrollBarRatio.removeAdjustmentListener(this);
		jScrollBarRatio.setValue(ratio);
		jScrollBarRatio.addAdjustmentListener(this);
		jLabelRatio.setText  ("Ratio: " + ratio);
	    if (pb.getIntParameter("Binarize") == 0)   jCheckBoxBinarize.setSelected(false);
	    if (pb.getIntParameter("Binarize") == 1)   jCheckBoxBinarize.setSelected(true);
	    
	}

	/**
	 * This method creates and shows the GUI
	 */
	@Override
	public void createIqmOperatorGUI() {
		//opGUIFrame = new ImgProcFuncJFrame();  //actionPerforemd events werden in dieser Klasse abgehandelt
		//opGUIFrame = new IqmRGBRelativeGUI();      //actionPerforemd events werden in dieser Klasse abgehandelt
		//this.getJContentPane();
		this.initialize(); //Aufruf von ImgProcFuncJFrame
		this.setTitle("RGBRelative");
		//PlanarImage pi = Tank.getCurrentTankImageAt(IqmTools.getCurrManagerImgIndex());
		//int numBands = pi.getNumBands();
		//Dimension dim = this.getSize();
		//this.setSize(dim.width, dim.height + 80);
		//this.setPreferredSize(new Dimension(800, 600));
		this.getJContentPane().add(getJToolBarChSelect(),   getGridBagConstraintsButtonGroupChSelect());
		this.getJContentPane().add(getJLabelText(),         getGridBagConstraintsText());
		this.getJContentPane().add(getJScrollBarRatio(),    getGridBagConstraintsRatio());
		this.getJContentPane().add(getJCheckBoxBinarize(),  getGridBagConstraintsBinarize());

		this.pack();
		//this.repaint();
		if (pb == null) pb = new ParameterBlockJAI("IqmRGBRelative");  //nur wenn das erste mal aufgerufen
		//pb = new ParameterBlockJAI("iqmrgbrelative"); //immer der default Block
		IqmTools.setCurrParameterBlockJAI(pb);
		setParameterValues();
	}

	//------------------------------------------------------------------------------
	private  GridBagConstraints getGridBagConstraintsButtonGroupChSelect(){
		GridBagConstraints gridBagConstraintsButtonGroupChSelect = new GridBagConstraints();
		gridBagConstraintsButtonGroupChSelect.gridx = 1;
		gridBagConstraintsButtonGroupChSelect.gridy = 4;
		//gridBagConstraintsButtonGroupChSelect.gridwidth = 3;
		gridBagConstraintsButtonGroupChSelect.insets = new Insets(0,15,0,0);  //top, left bottom right
		gridBagConstraintsButtonGroupChSelect.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsButtonGroupChSelect;
	}
	private  GridBagConstraints getGridBagConstraintsText(){
		GridBagConstraints gridBagConstraintsText = new GridBagConstraints();
		gridBagConstraintsText.gridx = 2;
		gridBagConstraintsText.gridy = 4;
		gridBagConstraintsText.gridwidth = 2;//?
		gridBagConstraintsText.insets = new Insets(15,0,5,0);  //top, left bottom right
		gridBagConstraintsText.anchor = GridBagConstraints.CENTER;
		//gridBagConstraintsText.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsText;
	}
	private  GridBagConstraints getGridBagConstraintsRatio(){
		GridBagConstraints gridBagConstraintsRatio = new GridBagConstraints();
		gridBagConstraintsRatio.gridx = 1;
		gridBagConstraintsRatio.gridy = 5;
		gridBagConstraintsRatio.gridwidth = 3;
		gridBagConstraintsRatio.insets = new Insets(15,0,0,0);  //top, left bottom right
		//gridBagConstraintsRatio.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsRatio;
	}
	private  GridBagConstraints getGridBagConstraintsBinarize(){
		GridBagConstraints gridBagConstraintsBinarize = new GridBagConstraints();
		gridBagConstraintsBinarize.gridx = 3;
		gridBagConstraintsBinarize.gridy = 6;
		gridBagConstraintsBinarize.anchor = GridBagConstraints.LINE_START;
		gridBagConstraintsBinarize.insets = new Insets(15,0,20,0);  //top, left bottom right
		//gridBagConstraintsBinarize.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsBinarize;
	}


	//------------------------------------------------------------------------------------------------------------------
	/**
	 * This method updates the GUI if needed
	 * This method overrides IqmOperatorJFrame
	 */
	@Override
	public void updateIqmOperatorGUI(){
	
	}
	
	/**
	 * This method initializes the Option: Extract red channel 	
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtChR() {
		//if (buttChR == null) {
			buttChR = new JRadioButtonMenuItem();
			buttChR.setText("R");
			buttChR.setToolTipText("calculate ratio for red channel");
			buttChR.addActionListener(this);
			buttChR.setActionCommand("parameter");
		//}
		return buttChR;
	}
	/**
	 * This method initializes the Option: Extract the green channel	
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtChG() {
		//if (buttChG == null) {
			buttChG = new JRadioButtonMenuItem();
			buttChG.setText("G");
			buttChG.setToolTipText("calculate ratio for green channel");
			buttChG.addActionListener(this);
			buttChG.setActionCommand("parameter");
		//}
		return buttChG;
	}
	/**
	 * This method initializes the Option: Extract the blue channel	
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtChB() {
		//if (buttChB == null) {
			buttChB = new JRadioButtonMenuItem();
			buttChB.setText("B");
			buttChB.setToolTipText("calculate ratio for blue channel");
			buttChB.addActionListener(this);
			buttChB.setActionCommand("parameter");
		//}
		return buttChB;
	}
	/**
	 * This method initializes the hidden option	
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtChHidden() {
		//if (buttChHidden == null) {
			buttChHidden  = new JRadioButtonMenuItem();
			buttChHidden .setText("Hidden");
			buttChHidden.setVisible(false);

		//}
		return buttChHidden;
	}
	/**
	 * This method initializes jJToolBarBar	
	 * 	
	 * @return javax.swing.JToolBar	
	 */
	private   JToolBar getJToolBarChSelect() {
//		if (jToolBarChSelect == null) {
			jToolBarChSelect = new JToolBar("Options");
			jToolBarChSelect.setOrientation(1); //
			jToolBarChSelect.add(getJRadioButtonMenuButtChR());
			jToolBarChSelect.add(getJRadioButtonMenuButtChG());			
			jToolBarChSelect.add(getJRadioButtonMenuButtChB());
			jToolBarChSelect.add(getJRadioButtonMenuButtChHidden());

			//jToolBarRGB.addSeparator();
			this.setButtonGroupChSelect();	//Gruppieren der JRadioButtons
//		}
		return jToolBarChSelect;
	}
	private  void setButtonGroupChSelect() {
		//if (ButtonGroup buttGroupChSelect == null) {
			buttGroupChSelect = new ButtonGroup();
			buttGroupChSelect.add(buttChR);
			buttGroupChSelect.add(buttChG);
			buttGroupChSelect.add(buttChB);
			buttGroupChSelect.add(buttChHidden);		
	}
	/**
	 * This method initializes jLabelText	
	 * @return javax.swing.JLabel	
	 */
	private  JLabel getJLabelText() {
		if (jLabelText	== null) {
			jLabelText    = new JLabel();
			jLabelText.setText("jLabelText");
		}
		return jLabelText	;
	}
	/**
	 * This method initializes jScrollBarRatio	
	 * @return javax.swing.JScrollBar	
	 */
	private  JPanel getJScrollBarRatio() {
		if (jScrollBarRatio	== null) {
			jScrollBarPanelRatio = new JPanel();	
			jScrollBarPanelRatio.setLayout(new BorderLayout());
			jScrollBarPanelRatio.setPreferredSize(new Dimension(250,18));			
			jScrollBarRatio	= new JScrollBar(JScrollBar.HORIZONTAL);
			jScrollBarRatio.setToolTipText("ratio of: selected plane/(sum of planes)");
			jScrollBarRatio.setValues(100, 1, 0, 100);
			jScrollBarRatio.addAdjustmentListener(this);
			jLabelRatio    = new JLabel();
			jLabelRatio.setPreferredSize(new Dimension(70,10));
			jScrollBarPanelRatio.add(jLabelRatio, BorderLayout.WEST);
			jScrollBarPanelRatio.add(jScrollBarRatio, BorderLayout.CENTER);		
		}
		return jScrollBarPanelRatio	;
	}

	/**
	 * This method initializes jCheckBoxBinarize	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private  JCheckBox getJCheckBoxBinarize() {
		if (jCheckBoxBinarize == null) {
			jCheckBoxBinarize = new JCheckBox();
			jCheckBoxBinarize.setText("Binarize");
			jCheckBoxBinarize.addActionListener(this);			
			jCheckBoxBinarize.setActionCommand("parameter");			
		}
		return jCheckBoxBinarize;
	}

	
	//--------------------------------------------------------------------------------------------
	public void actionPerformed(ActionEvent e) {
		//System.out.println("IqmRGBRelativeGUI: event e: " +e.getActionCommand());
		if ("reset".equals(e.getActionCommand())) {
			ParameterBlockJAI pb = new ParameterBlockJAI("IqmRGBRelative".toLowerCase());
			IqmTools.setCurrParameterBlockJAI(pb);
			this.setParameterBlock(pb);
			this.setParameterValues();
			//this.updateIqmOperatorGUI();  //if necessary here or some lines above
			this.reset();
		}
		if ("parameter".equals(e.getActionCommand())) {
			if (buttChR.isSelected()) jLabelText.setText("Ratio = R/(R+G+B)");
			if (buttChG.isSelected()) jLabelText.setText("Ratio = G/(R+G+B)");
			if (buttChB.isSelected()) jLabelText.setText("Ratio = B/(R+G+B)");
			this.setParameterBlock();
			//this.updateIqmOperatorGUI();  //if necessary here or some lines above
		}

		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}

	}
	@Override
	public void adjustmentValueChanged(AdjustmentEvent e) {
		Object obE = e.getSource();
		if (obE instanceof JScrollBar){
			jScrollBarRatio.removeAdjustmentListener(this);
			
			//ScrollBars-----------------------------------------------------------------------------
			if (obE == jScrollBarRatio){
				ratio = jScrollBarRatio.getValue();
				jLabelRatio.setText  ("Ratio: " + ratio);
			}
			jScrollBarRatio.addAdjustmentListener(this);
	
		}
		this.setParameterBlock();
		//this.updateIqmOperatorGUI();  //if necessary here or some lines above
		
		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}
	}
	//-------------------------------------------------------------------------------------------------
	public static void main() {

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				IqmRGBRelativeGUI gui = new IqmRGBRelativeGUI();
				gui.createIqmOperatorGUI();

			}
		});
	}



	
}//END
